<template>
    <div class="form">
        <div class="username">
            <div class="icon icon-username">
                <van-icon :color="usernameClass == 'focus' ? color : inactive" :name="iconName" />
            </div>
            <div :class="['placeholder', { focus: usernameClass == 'focus' }]">
                {{ placeholder }}
            </div>
            <input :value="modelValue" @input="$emit('update:modelValue', $event.target.value)"
                :class="{ inputFocus: usernameClass == 'focus' }" :type="type" @focus="focus($event.target.value)"
                @blur="blur($event.target.value)">
        </div>
    </div>
</template>

<script setup>
import { ref, onMounted, watch } from 'vue';

const props = defineProps({
    // 用户名
    iconName: {
        type: String,
        default: 'friends'
    },
    // 图标颜色
    color: {
        type: String,
        default: '#2196F3'
    },
    inactive: {
        type: String,
        default: '#8C8C8C'
    },
    placeholder: {
        type: String,
        required: true,
    },
    modelValue: {
        type: String,
        required: true,
        default: ''
    },
    type: {
        type: String,
        default: 'text'
    },
    disabled: {
        type: Boolean,
    }
})

const usernameClass = ref('')

watch(() => props.modelValue, (n, o) => {
    focus(n)
})



function focus(params) {
    if (params !== '') {
        // 有值
        usernameClass.value = 'focus'
    } else {
        // 无值
        usernameClass.value = 'focus'
    }
}

function blur(params) {
    if (params !== '') {
        // 有值
        usernameClass.value = 'focus'
    } else {
        // 无值
        usernameClass.value = ''

    }

}

</script>

<style lang="less" scoped>
.form {
    width: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;

    input {
        width: 100%;
        height: 50px;
        border-radius: 3px;
        border: 1px solid #E7E7E7;
        box-sizing: border-box;
        padding: 0 40px;
        margin: 10px 0;
        outline: none;
        background: none;
        font-size: 22px;
        transition: all 0.3s;
    }

    .username,
    .password {
        position: relative;

        .icon {
            position: absolute;
            left: 10px;
            top: 50%;
            transform: translateY(-50%);
            font-size: 20px;
        }

        .placeholder {
            position: absolute;
            left: 40px;
            top: 50%;
            transform: translateY(-50%);
            font-size: 16px;
            color: #000;
            transition: all 0.3s;
        }

        .focus {
            left: 30px;
            top: 15%;
            font-size: 12px;
            padding: 0px 3px;
            background-color: #fff;
        }

        .inputFocus {
            border: 1px solid #000;
        }

        .icon-right {
            position: absolute;
            right: 10px;
            top: 50%;
            transform: translateY(-50%);
            font-size: 20px;
        }
    }

}
</style>